package com.shensi.appCollect.controller;

import com.google.gson.Gson;
import com.shensi.appCollect.param.request.CustomMessages;
import com.shensi.appCollect.pojo.CustomerLabel;
import com.shensi.appCollect.pojo.LabelRule;
import com.shensi.appCollect.pojo.dto.LabelRuleDto;
import com.shensi.appCollect.service.LabelService;
import com.shensi.appCollect.utils.CodeMsg;
import com.shensi.appCollect.utils.ResultData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.*;
import java.util.stream.Collectors;


/*
 * <Title> LabelController </Title>
 * <Description> 标签模块 </Description>
 * @Date 2021年7月15日
 */
@Controller
//@QuJiaRequest()
@CrossOrigin
@Api(tags = "标签")
public class LabelController {
    private static Logger logger = (Logger) LoggerFactory.getLogger(LabelController.class);

    @Resource
    private LabelService labelService;

    @GetMapping("/ruleLabelList")
    @ResponseBody
    @ApiOperation(value = "渠道列表", notes = "渠道列表")
    public String ruleLabelList(@ApiParam(value = "油企id", required = true) @RequestParam Integer enterpriseId) {
        Gson gson = new Gson();
        ResultData resultData = null;
        try {
            LabelRule labelRule = new LabelRule();
            labelRule.setEnterpriseId(enterpriseId);
            labelRule.setDelFlag("0");
            labelRule.setDataFlag(0);
            List<LabelRule> labelRuleList = labelService.getLabelRuleList(labelRule);
            labelRuleList = labelRuleList.stream().filter(i -> i.getStartDate().before(new Date()) && i.getEndDate().after(new Date())).collect(Collectors.toList());
            Map<String, List<LabelRule>> collect =
                    labelRuleList.stream().collect(Collectors.groupingBy(LabelRule::getLabelRuleType));
            List<Map<String, Object>> list = new ArrayList<>();
            Set<String> strings = collect.keySet();
            for (String string : strings) {
                Map<String, Object> m = new HashMap<>();
                m.put("carTypeName", string);
                m.put("carList", collect.get(string));
                list.add(m);
            }

            resultData = ResultData.success(list);
        } catch (Exception e) {
            logger.info("渠道查询错误: " + e.getMessage());
            resultData = ResultData.error(CodeMsg.YEWU_FAIL);
        }
        return gson.toJson(resultData);
    }

    //用户扫码进标签
    @RequestMapping(value = "/addUserToLabel", method = RequestMethod.GET)
    @ResponseBody
    //@ApiOperation(value = "用户扫码进标签", httpMethod = "GET", notes = "用户扫码进标签")
    public String addUserToLabel(@ApiParam(value = "油站ID", required = true) @RequestParam Integer stationId,
                                 @ApiParam(value = "手机号", required = true) @RequestParam String phone,
                                 @ApiParam(value = "标签ID", required = true) @RequestParam String labelId,
                                 @ApiParam(value = "创建人", required = true) @RequestParam String createBy) {
        Gson gson = new Gson();
        ResultData resultData = null;       //返回结果集
        try {
            CustomerLabel customerLabel = new CustomerLabel();
            customerLabel.setStationId(stationId);
            customerLabel.setPhone(phone);
            //获取该标签用户信息
            CustomerLabel c = labelService.getCustomerLabel(customerLabel);
            if (c != null) {
                customerLabel.setLabelId(labelId);
                customerLabel.setUpdateBy(createBy);
                customerLabel.setUpdateTime(new Date());
                labelService.updateCustomerLabel(customerLabel);
            } else {
                customerLabel.setCreateBy(createBy);
                customerLabel.setCreateTime(new Date());
                customerLabel.setDelFlag("0");
                customerLabel.setLabelId(labelId);
                labelService.insertCustomerLabel(customerLabel);
            }
            resultData = ResultData.success(CodeMsg.SUCCESS);
        } catch (Exception e) {
            logger.info("用户扫码进标签错误信息: " + e.getMessage());
            resultData = ResultData.error(CodeMsg.YEWU_FAIL);
        }
        return gson.toJson(resultData);
    }

    /**
     * 修改渠道是否需要审核
     */
    @PostMapping(value = "/updateIsPush")
    public ResultData updateIsPush(@Valid @RequestBody LabelRuleDto labelRuleDto) {
        return ResultData.success(labelService.updateIsPush(labelRuleDto));

    }

    /**
     * 发送模针对用户
     */
    @PostMapping(value = "/sendMessageToUser")
    @ResponseBody
    public ResultData sendMessageToUser(@RequestBody CustomMessages customMessages) {
        return labelService.sendMessageToUser(customMessages);

    }
}
